package com.example.myanimator.shadowLayer;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/30
 * @describe 文本渐变
 */
public class DrawTextShadowFlash extends View {

    private Path mCirclePath, mDstPath, linePath, mLineDstPath;
    private Paint mPaint;
    private Bitmap mBitmap;
    private String mContentStr;
    private Rect mContentBounds = new Rect();//承载文本宽度
    private float mCurAnimValue;
    private LinearGradient mLinearGradient;

    public DrawTextShadowFlash(Context context) {
        super(context);
    }

    public DrawTextShadowFlash(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawTextShadowFlash(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);

        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setDither(true); // 抗抖动
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(100);//设置文字大小
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.BLACK);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawColor(Color.BLACK);
        int[] colors = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00, 0xff00ffff};
        float[] pos = {0f, 0.2f, 0.4f, 0.6f, 1.0f};
        LinearGradient multiGradient = new LinearGradient(0, 100, getWidth() / 2,
                0, colors, pos, Shader.TileMode.CLAMP);//末端延申
        mPaint.setShader(multiGradient);
        canvas.drawRect(0, 0, getWidth(), 100, mPaint);
        mContentStr = "渐变字体，不知道好看不";
        //普通渐变字体   //  float top = baseLineY + fm.top;  要想文字在彩带下面 所以基线为 100-mPaint.getFontMetrics().top
        canvas.drawText(mContentStr, 0, 100 - mPaint.getFontMetrics().top, mPaint);

        //动画渐变字体
        if (mLinearGradient == null) {
            canvas.drawText(mContentStr, 0, 100 - mPaint.getFontMetrics().top, mPaint);
            mPaint.getTextBounds(mContentStr, 0, mContentStr.length(), mContentBounds);
            startWaveAnimator();
            createLinearGradient(mContentBounds.width());
        } else {
            Matrix matrix = new Matrix();
            matrix.setTranslate(mCurAnimValue, 0);
            mLinearGradient.setLocalMatrix(matrix);

            mPaint.setShader(mLinearGradient);
        }
        canvas.drawText(mContentStr, 0, 200 - mPaint.getFontMetrics().top, mPaint);


    }


    private void startWaveAnimator() {
        ValueAnimator animator = ValueAnimator.ofFloat(0,mContentBounds.width());
//        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurAnimValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(3000);
        animator.start();
    }

    private void createLinearGradient(int length) {
        mLinearGradient = new LinearGradient(-length, 0,0, 0, new int[]{
                0x00000000, 0xff00ff00, 0x00000000}, new float[]{0, 0.5f, 1}, Shader.TileMode.CLAMP);
    }
}
